package com.sky.Task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.OrdermmMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrdersTask {
    @Autowired
    private OrdermmMapper ordermmMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void OrderTimeout(){
        log.info("处理超时订单：{}", LocalDateTime.now());
        //获取下单时间到超时时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime time = now.minusMinutes(15);
        //查询数据库是否有超时订单
        List<Orders> orderTime = ordermmMapper.scelectTimeout(Orders.PAID,time);
        //判断是否超时
        if (orderTime !=null && orderTime.size()>0){
            for (Orders orders : orderTime) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("超时，自动取消");
                ordermmMapper.cancel(orders);
            }
        }
    }
    @Scheduled(cron = "0 0 1 * * ?")
    public void OrderDelivery(){
            log.info("处理超时订单：{}", LocalDateTime.now());
            //获取下单时间到超时时间
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime time = now.minusMinutes(60);
        //查询派送中的订单
        List<Orders> orderTime = ordermmMapper.scelectTimeout(Orders.DELIVERY_IN_PROGRESS,time);
        //判断是为空
        if (orderTime !=null && orderTime.size()>0){
            for (Orders orders : orderTime) {
                orders.setStatus(Orders.COMPLETED);
                ordermmMapper.updateByid(orders);
            }
        }

    }
}
